﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Com.Toook.Common;
using System.Data.SQLite;
using System.Text;

public partial class joke_index : BaseShowPage
{
    private int pageSize = 10;
    protected string JokeListHtml = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitJokeList();
        }
    }
    private void InitJokeList()
    {
        DataRow row = PageData.SelectParamRow("typeCode='joke'");

        Master.SetTitleKeyDesc(row);

        string siteId = row[PageColumn.newsSiteId].ToString();
        string[] ids = siteId.Split(',');        
        string sqlStart = "select id, title, content from JokePublish ";
        string sqlEnd = " order by id desc limit 0," + pageSize.ToString();
        string sql = "";
        StringBuilder sbHtml = new StringBuilder(8192);
        SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(conn);
        SQLiteDataReader reader = null;

        try
        {
            conn.Open();
            foreach (string sid in ids)
            {
                sql = sqlStart + " where type=" + sid.Trim() + " " + sqlEnd;
                cmd.CommandText = sql;
                bool isFirst = true;
                try
                {
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        if (isFirst)
                        {
                            isFirst = false;
                            DataRow rowSite = PageData.SelectParamRow("typeId=" + sid);
                            string siteName = rowSite[PageColumn.typeName].ToString();
                            sbHtml.Append("<div class=\"jokeSite\" id=\"joke").Append(sid).Append("\"><h3>");
                            sbHtml.Append(siteName).Append("</h3><dl>");
                        }

                        sbHtml.Append("<dt><a href=\"content.aspx?id=").Append(reader["id"].ToString()).Append("\" target=\"_blank\">");
                        sbHtml.Append(reader["title"].ToString()).Append("</a></dt>");
                       
                        sbHtml.Append("<dd>").Append(reader["content"].ToString()).Append("</dd>");                        
                    }
                    reader.Close();
                    if (!isFirst)//isFirst = false;说明已经读取到数据，加上结束标签
                    {
                        sbHtml.Append("</dl></div>");
                    }
                   
                }
                catch (Exception exRead)
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    LogHelper.Log.Error("读取joke数据出错。sql=" + sql, exRead);
                }
            }
        }
        finally
        {
            SqliteHelper.CloseDbResource(conn, reader);
        }
        JokeListHtml = sbHtml.ToString();
    }
}
